package grafica;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import controladores.ControladorVentanaListarMascotas;

public class ListarMascotas {

	private JFrame frmListadoDeMascotas;
	private JTable table;
	private JTextField textField;
	private final Action action = new SwingAction();
	private ControladorVentanaListarMascotas controlador = new ControladorVentanaListarMascotas();
	private Object[][] data=null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ListarMascotas window = new ListarMascotas();
					window.frmListadoDeMascotas.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ListarMascotas() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmListadoDeMascotas = new JFrame();
		frmListadoDeMascotas.setTitle("Listado de mascotas");
		frmListadoDeMascotas.setBounds(100, 100, 450, 300);
		frmListadoDeMascotas.getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 36, 409, 215);
		frmListadoDeMascotas.getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Apodo", "Raza", "Cedula Due\u00F1o"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, Integer.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.getColumnModel().getColumn(2).setPreferredWidth(91);
		scrollPane.setViewportView(table);
		
		JButton btnListarMascotas = new JButton("Listar Mascotas");
		btnListarMascotas.setAction(action);
		btnListarMascotas.setBounds(323, 7, 86, 23);
		frmListadoDeMascotas.getContentPane().add(btnListarMascotas);
		
		JLabel lblCedulaDelDueo = new JLabel("Cedula del due\u00F1o:");
		lblCedulaDelDueo.setBounds(10, 11, 138, 14);
		frmListadoDeMascotas.getContentPane().add(lblCedulaDelDueo);
		
		textField = new JTextField();
		textField.setBounds(183, 8, 130, 20);
		frmListadoDeMascotas.getContentPane().add(textField);
		textField.setColumns(10);
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Listar");
			putValue(SHORT_DESCRIPTION, "Lista las mascotas");
		}
		public void actionPerformed(ActionEvent e) {
			
			int cedula = Integer.parseInt(textField.getText());
			
			data = controlador.ListarMascotas(cedula);
			
			table.setModel(new DefaultTableModel(data,new String[] {"Apodo", "Raza", "Cedula Due\u00F1o"}));
		}
	}
	
	public void show ()
	{
		frmListadoDeMascotas.setVisible(true);
	}
}
